package cc.ipost.ipost;

import org.json.JSONObject;

import cc.ipost.ipost.data.User;
import android.annotation.SuppressLint;
import android.app.Application;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceActivity;
import android.util.Log;

@SuppressLint("WorldWriteableFiles")
public class MyApplication extends Application {
	public User user = null;
	
	public boolean checkUser() {
		SharedPreferences userPre = getSharedPreferences("user", PreferenceActivity.MODE_WORLD_WRITEABLE);
		String ID = userPre.getString("ID", null);
		if (ID != null) {
			user = new User(ID);
			return true;
		}
		return false;
	}
	
	public void initUser(JSONObject user) {
		this.user = new User(user);
		SharedPreferences userPre = getSharedPreferences("user", PreferenceActivity.MODE_WORLD_WRITEABLE);
		Editor e = userPre.edit();
		e.putString("ID", this.user.ID);
		e.commit();
		Log.v("init user", this.user.toString());
	}
	
	public void removeUser() {
		SharedPreferences userPre = getSharedPreferences("user", PreferenceActivity.MODE_WORLD_WRITEABLE);
		Editor e = userPre.edit();
		e.putString("ID", null);
		e.commit();
		this.user = null;
		
	}
	

}
